package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayConfig;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayConfigMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayConfigParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网关配置表 服务实现类
 *
 * @author ssy
 * @date 2022/10/31 15:29
 */
@Service
public class BizGatewayConfigServiceImpl extends ServiceImpl<BizGatewayConfigMapper, BizGatewayConfig> implements BizGatewayConfigService {

    @Override
    public PageResult<BizGatewayConfig> page(BizGatewayConfigParam bizGatewayConfigParam) {

        // 构造条件
        LambdaQueryWrapper<BizGatewayConfig> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizGatewayConfig> list(BizGatewayConfigParam bizGatewayConfigParam) {

        // 构造条件
        LambdaQueryWrapper<BizGatewayConfig> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(BizGatewayConfigParam bizGatewayConfigParam) {

        // 将dto转为实体
        BizGatewayConfig bizGatewayConfig = BizGatewayConfig.builder().build();
        BeanUtil.copyProperties(bizGatewayConfigParam, bizGatewayConfig);

        this.save(bizGatewayConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayConfigParam bizGatewayConfigParam) {
        this.removeById(bizGatewayConfigParam.getId());
    }

    @Override
    public void edit(BizGatewayConfigParam bizGatewayConfigParam) {

        // 根据id查询实体
        BizGatewayConfig bizGatewayConfig = this.queryBizGatewayConfig(bizGatewayConfigParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayConfigParam, bizGatewayConfig);

        this.updateById(bizGatewayConfig);
    }

    @Override
    public BizGatewayConfig detail(BizGatewayConfigParam bizGatewayConfigParam) {
        return this.queryBizGatewayConfig(bizGatewayConfigParam);
    }

    /**
     * <p>网关id查询网关配置信息</p>
     */
    @Override
    public List<BizGatewayConfig> gatewayConfigList(Integer gatewayId) {
        return this.baseMapper.getConfigByGatewayId(gatewayId);
    }


    /**
     * 获取网关配置表
     *
     * @author ssy
     * @since 2022/10/31 15:29
     */
    private BizGatewayConfig queryBizGatewayConfig(BizGatewayConfigParam bizGatewayConfigParam) {
        BizGatewayConfig bizGatewayConfig = this.getById(bizGatewayConfigParam.getId());
        return bizGatewayConfig;
    }
}
